#include <QString>
#include <QXmlStreamReader>
#include <QDebug>
#include <QXmlStreamAttributes>
#include <QMap>
#include "Item.h"
#include "global.h"
//
Item::Item() 
{
	id=-1;
	binds=0;
	slot=0;

	numSlots=0;
	socketBonus=0;
	name="";

	intellect=0;
	spirit=0;
	stamina=0;
	
	shadowdamage=0;
	spellcrit=0;
	spelldmg=0;
	spellhaste=0;
	spellhitrating=0;
	
	procPseudodamage=0;
}

QString Item::getUrl(){
	if (id>0){
		QString url ="http://wow.allakhazam.com/dev/wow/item-xml.pl?witem=";
		url+=QString::number(id);//+id; 
		//qDebug()<<"Item::getUrl: "<<url;
		return url;
	} else {
		return QString::null;
	}
}

void Item::downloadFinished(QString data){
	QXmlStreamReader xml;
	xml.addData(data);
	QMap<QString, QString> stats;
	while (!xml.atEnd()) {
		xml.readNext();
		if (xml.isStartElement()){
			while(!xml.atEnd()){
				xml.readNext();
				if (xml.isStartElement()){
					QString name = xml.name().toString();
					// Parse raw stats
					if (name == "stats"){
						while(!xml.atEnd()){
							xml.readNext();
							if (xml.isStartElement()){
								QString statName = xml.name().toString();
								QString statText = xml.readElementText();
								stats.insert(statName, statText);
							}
						}
					}
					QString text = xml.readElementText();
					stats.insert(name, text);
					//qDebug()<<"item token "<<name<<"text:"<<text;
				}
			}
		}
	}
	// Parse loaded parameters
	binds = intOfMapIndex(stats, "binds");
	slot = intOfMapIndex(stats, "slot");

	numSlots = intOfMapIndex(stats, "slots");
	socket[0] = intOfMapIndex(stats, "socket_1");
	socket[1] = intOfMapIndex(stats, "socket_2");
	socket[2] = intOfMapIndex(stats, "socket_3");
	socketBonus = intOfMapIndex(stats, "socketbonus");
	name = stats["name1"];

	intellect = intOfMapIndex(stats, "intellect");
	spirit  = intOfMapIndex(stats, "spirit");
	stamina = intOfMapIndex(stats, "stamina");
	
	shadowdamage = intOfMapIndex(stats, "shadowdmg");
	spellcrit = intOfMapIndex(stats, "spellcrit");
	spelldmg = intOfMapIndex(stats, "spelldmg");
	spellhaste = intOfMapIndex(stats, "spellhaste");
	spellhitrating = intOfMapIndex(stats, "spellhitrating");
}
